package metricmaster.app;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import metricmaster.model.Project;

public class ProjectRepositoryFileImpl implements ProjectRepository {

	public ProjectRepositoryFileImpl() {
		
	}

	@Override
	public boolean saveProject(Project project) {
		FileOutputStream os;
		boolean success = true;
		String fileName = project.getName();
		try {
                        
			os = new FileOutputStream(System.getProperty("user.home") + File.separator + fileName + ".xml");
			XMLEncoder encoder = new XMLEncoder(os);
			encoder.writeObject(project);
			encoder.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		}
		
		return success;
	}

    @Override
    public Project loadProject(String projectName) {
                FileInputStream is;
                Project project = null;
            try {
                
                is = new FileInputStream(System.getProperty("user.home") + File.separator + projectName + ".xml");
                XMLDecoder decoder = new XMLDecoder(is);
                project = (Project)decoder.readObject();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ProjectRepositoryFileImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            return project;
    }

}
